package cn.com.lyb.asyncTask.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步任务执行配置
 * @author wangchen
 */
@Configuration
public class AsyncConfig implements AsyncConfigurer {

    @Value("${asyncTaskCorePoolSize:5}")
    private Integer asyncTaskCorePoolSize;

    @Value("${asyncTaskMaxPoolSize:10}")
    private Integer asyncTaskMaxPoolSize;

    @Value("${asyncTaskQueueSize:20}")
    private Integer asyncTaskQueueSize;

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setThreadNamePrefix("asyncTask-");
        taskExecutor.setCorePoolSize(asyncTaskCorePoolSize);
        taskExecutor.setMaxPoolSize(asyncTaskMaxPoolSize);
        taskExecutor.setQueueCapacity(asyncTaskQueueSize);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
